package codec

import (
	"errors"
	"github.com/vmihailenco/msgpack/v5"
)

// ############################################### 接 口 封 装 ##########################################################

type MsgPackCodec struct{}

// 序列化
func (m MsgPackCodec) Marshal(data interface{}) ([]byte, error) {
	return msgpack.Marshal(data)
}

// 反序列化
func (m MsgPackCodec) Unmarshal(data []byte, v interface{}) error {
	return msgpack.Unmarshal(data, v)
}

func (m MsgPackCodec) String() string {
	return "MsgPackCodec"
}

// 封装公共方法调用
func MsgpackMarshal(v interface{}) ([]byte, error) {
	if v == nil {
		return nil, errors.New("unable to marshal nil v!")
	}
	return msgpack.Marshal(v)
}

func MsgpackUnmarshal(data []byte, v interface{}) error {
	if data == nil {
		return errors.New("unable to unmarshal nil d!")
	}
	return msgpack.Unmarshal(data, v)
}
